<template>
  <div class="app-container">

    <div class="tree-container">
      <el-tree
        class="filter-tree"
        :data="data"
        :props="defaultProps"
        node-key="id"
        @node-click="handleNodeClick"
      />
    </div>

    <div class="divider">
      <el-divider />
      <el-divider />
    </div>

    <div class="table-container">

      <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
        <el-form-item label="姓名" prop="nickName">
          <el-input v-model="queryParams.nickName" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="角色" prop="postName">
          <el-select v-model="queryParams.postName" placeholder="请选择">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>

        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['system:inf:add']"
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >选择班组成员</el-button>
        </el-col>
        <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
      </el-row>

      <el-table
        v-loading="loading"
        :data="infList"
        tooltip-effect="dark"
      >
        <!-- @selection-change="handleSelectionChange" -->
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column type="index" label="序号" align="center" />
        <el-table-column label="姓名" align="center" prop="nickName" />
        <el-table-column label="角色" align="center" prop="postName" />
        <el-table-column label="所属班组" align="center" prop="deptName" />
        <el-table-column label="所属区队" align="center" prop="parentDeptName" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              v-hasPermi="['system:inf:edit']"
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改</el-button>
            <el-button
              v-hasPermi="['system:inf:remove']"
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分割线 -->
      <el-divider />

      <!-- 分页 -->
      <div class="block" style="float:right">
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNumber"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>

    </div>

    <!-- 添加班组信息对话框 -->
    <el-dialog :title="dialog_insert_title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="INSERT" :rules="rules" label-width="80px">
        <el-form-item label="班组" prop="Insert_team">
          <el-cascader
            v-model="INSERT.Insert_team"
            :options="tree_data"
            @change="handleChange"
          />
        </el-form-item>
      </el-form>
      <el-divider />
      <el-table
        ref="multiTable"
        :data="tableData"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column prop="nickName" label="姓名" width="180" />
        <el-table-column prop="jobNum" label="工号" width="180" />
        <el-table-column prop="deptName" label="所属区队" />
      </el-table>
      <!-- 分页 -->
      <pagination
        v-show="Innertotal > 0"
        :total="Innertotal"
        :page.sync="InnerqueryParams.pageNumber"
        :limit.sync="InnerqueryParams.pageSize"
        @pagination="InnergetList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 编辑班组信息对话框 -->
    <el-dialog
      title="编辑班组"
      :visible.sync="changeVisible"
      width="30%"
    >
      <el-divider />
      <el-descriptions :model="changeData">
        <el-descriptions-item label="姓名">{{ changeData.nickName }}</el-descriptions-item>
        <el-descriptions-item label="所属区队">{{ changeData.parentDeptName }}</el-descriptions-item>
        <el-descriptions-item label="班组">{{ changeData.deptName }}</el-descriptions-item>
        <el-descriptions-item label="班组长">{{ changeData.morName }}</el-descriptions-item>
      </el-descriptions>
      <el-form :model="changeData">
        <el-form-item prop="teamrole" label="班组角色">
          <el-select v-model="changeData.postName">
            <el-option v-for="item in change_options" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item prop="other" label="其他情况">
          <el-input
            v-model="changeData.otherExplain"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>

      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="changeVisible = false">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
        <!-- @click="changeVisible = false" -->
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listInf, delInf, updateInf, getTree, InnerlistInf, addInf } from '@/api/new/team_inf'

export default {
  name: 'Inf',
  data() {
    return {

      // 树形控件
      data: [],

      tree_data: [],

      defaultProps: {
        label: 'label',
        children: 'children'
      },

      // 编辑班组信息对话框按钮
      changeVisible: false,
      // 编辑对话框信息获取
      changeData: [],

      // 下拉选项
      options: [{
        value: '班长',
        label: '班长'
      }, {
        value: '普通员工',
        label: '普通员工'
      }],

      // 编辑选项
      change_options: [{
        value: '班组长',
        label: '班组长'
      }, {
        value: '普通员工',
        label: '普通员工'
      }],

      // 后端传入的所有班组信息作为options
      team_options: [],

      // 编辑输入文本框
      change_textarea: '',

      // 新建弹出框标题
      dialog_insert_title: '请选择班组人员',

      // 默认value  下拉框中的默认
      value: '',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,

      Innertotal: 0,

      InnerqueryParams: {
        pageNumber: 1,
        pageSize: 10,
        deptId: 0
      },
      // 班组信息表格数据
      infList: [],

      // 添加班组信息表格
      tableData: [],

      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNumber: 1,
        pageSize: 10,
        nickName: undefined,
        postName: undefined,
        deptId: 0
      },

      INSERT: {
        Insert_team: ''
      },

      SELECTION: [],

      // 表单参数
      form: {},
      // 表单校验
      rules: {
        Insert_team: [
          { required: true, message: 'Please select an option', trigger: 'change' }
        ]
      }
    }
  },

  created() {
    // 树形控件
    this.init()
    this.getList()
    this.InnergetList()
  },

  methods: {
    /** 查询班组信息列表 */
    getList() {
      this.loading = true
      console.log(this.queryParams)
      listInf(this.queryParams).then(response => {
        this.infList = response.data.teamClassInfList
        this.total = response.data.total
        this.loading = false
      })
    },

    // 级联
    InnergetList() {
      InnerlistInf(this.InnerqueryParams).then(response => {
        this.tableData = response.data.teamClassInfList
        this.Innertotal = response.data.total
      })
    },

    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        teamName: null,
        teamRole: null,
        teamTeam: null,
        teamDistrictTeam: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNumber = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.SELECTION = selection
      console.log(this.SELECTION)
      this.ids = selection.map(item => item.userId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // 导入后端传入的所有班组的选项
      this.reset()
      this.open = true
    },
    // 重置编辑表单中的数据
    resetChangeData() {
      this.changeData = {
        nickName: '',
        parentDeptName: '',
        postName: '',
        deptName: '',
        otherExplain: '',
        userId: '',
        jobNum: '',
        parentId: '',
        deptRank: '',
        morName: ''
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.resetChangeData()
      // 需要注意的是 row前面不要需要加this  但是changeDate需要
      console.log(row)
      this.changeData.nickName = row.nickName
      this.changeData.parentDeptName = row.parentDeptName
      this.changeData.deptName = row.deptName
      this.changeData.postName = row.postName
      this.changeData.userId = row.userId
      this.changeData.deptRank = row.deptRank
      this.changeData.parentId = row.parentId
      this.changeData.jobNum = row.jobNum
      this.changeData.userId = row.userId
      this.changeData.morName = row.morName
      console.log(this.changeData)
      this.changeVisible = true
    },
    // 修改 确定按钮
    update() {
      this.changeVisible = false
      updateInf(this.changeData).then(response => {
      })
    },

    submitForm(selection) {
      addInf(this.SELECTION, this.InnerqueryParams.deptId).then(response => {
      })
      this.open = false
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      console.log(row)
      this.$modal.confirm('是否确认删除班组信息编号为"' + ids + '"的数据项？').then(function() {
        return delInf(row)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => { })
    },

    // 分页
    // pageSize改变时触发的事件
    handleSizeChange(val) {
      this.queryParams.pageNumber = 1
      this.queryParams.pageSize = val
    },
    // 当前页码发生改变时触发的事件
    handleCurrentChange(val) {
      this.queryParams.pageNumber = val
    },

    // 树形控件   其中Data  为后端发送过来的数据
    init() {
      let Data = []

      getTree().then(response => {
        // 先接受后端传入的原始数据
        Data = response.data
        console.log(Data)
        this.data = this.formatData(response.data, 0)
        this.tree_data = this.formattree_data(response.data, 0)
        console.log(this.tree_data)
      })
    },

    // 生成数据
    formatData(data, parentId) {
      const result = []
      for (let i = 0; i < data.length; i++) {
        if (data[i].parentId === parentId) {
          const item = {
            id: data[i].deptId,
            label: data[i].deptName,
            children: this.formatData(data, data[i].deptId)
          }
          result.push(item)
        }
      }
      return result
    },

    formattree_data(data, parentId) {
      const result = []
      // value: deptId  label: deptName
      for (let i = 0; i < data.length; i++) {
        if (data[i].parentId === parentId) {
          if (data[i].deptRank === 3) {
            const item = {
              value: data[i].deptId,
              label: data[i].deptName
            }
            result.push(item)
          } else {
            const item = {
              value: data[i].deptId,
              label: data[i].deptName,
              children: this.formattree_data(data, data[i].deptId)
            }
            result.push(item)
          }
        }
      }
      return result
    },

    handleChange(node) {
      console.log(node[2])
      this.InnerqueryParams.deptId = node[2]
      this.InnergetList()
    },
    // {
    //   id:data[i].id,
    //   label:data[i].label,
    //   children: this.formatData(data,data[i].id)
    // }
    // 点击节点触发相应事件  树形控件
    handleNodeClick(node) {
      // const parentId = this.findParentId(this.data, node.id)
      this.queryParams.deptId = node.id
      console.log(node.id)
      console.log(this.queryParams)
      this.getList()
    }
  }
}
</script>

<style scoped>
.app-container {
  display: flex;
}

.tree-container {
  width: 20%;
}

.divider {
  width: 1px;
  background-color: #ccc;
  height: 100%;
  margin-left: 10px;
  margin-right: 10px;
}

.table-container {
  width: 79%;
}
</style>

 <!-- extractLeafOptions(nodes) {
      for (const node of nodes) {
        if (node.children && node.children.length > 0) {
          this.extractLeafOptions(node.children)
        } else {
          this.team_options.push({ id: node.id, label: node.label })
        }
      }
    }, -->

     <!-- // 树形控件
    findParentId(data, nodeId) {
      for (let i = 0; i < data.length; i++) {
        if (data[i].children && data[i].children.length > 0) {
          if (this.hasChildNode(data[i].children, nodeId)) {
            return data[i].id
          } else {
            const parentId = this.findParentId(data[i].children, nodeId)
            if (parentId !== null) {
              return parentId
            }
          }
        }
      }
      return null
    }, -->
 <!-- // 树形控件
    hasChildNode(children, nodeId) {
      for (let i = 0; i < children.length; i++) {
        if (children[i].id === nodeId) {
          return true
        }
      }
      return false
    } -->

    <!-- <el-form-item prop="name" label="姓名">
          <el-input v-model="changeData.nickName" style="width :77%" />
        </el-form-item>
        <el-form-item prop="districtteam" label="所属区队">
          <el-input v-model="changeData.parentDeptName" style="width :70%" />
        </el-form-item>
        <el-form-item prop="team" label="班组">
          <el-input v-model="changeData.deptName" style="width :77%" />
        </el-form-item>
        <el-form-item prop="teamleader" label="班组长">
          <el-input v-model="changeData.teamleader" style="width :73%" />
        </el-form-item> -->
